﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileProtector
{
    public partial class frmMain : Form
    {
        //Settings
        Libraries.SettingParams sp;

        //Temp
        string tmp = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\temp";

        public frmMain()
        {
            InitializeComponent();
            //Settings Generator
            sp = new Libraries.SettingParams(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
            //Load List
            LoadList();

            //Generic
            this.Text = "Welcome " + sp.Name + " - " + this.Text;
            lblVaultDir.Text = sp.VaultDirectory;

            //Create Temp
            if (!Directory.Exists(tmp))
                Directory.CreateDirectory(tmp);
        }
                
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                frmPassword.ActiveForm.Hide();
                frmSettings.ActiveForm.Hide();
            }
            catch { }
        }

        //Exit
        private void btnClose_Click(object sender, EventArgs e)
        {
            //Delete Temp
            if (Directory.Exists(tmp))
                Directory.Delete(tmp);

            Application.Exit();
        }

        //Show Settings
        private void btnSettings_Click(object sender, EventArgs e)
        {
            frmSettings frm = new frmSettings();
            frm.Show();
            this.Close();
        }

        //Validate
        public bool ValidateCredential()
        {
            if (sp.Password != this.strPassword)
            {
                MessageBox.Show("Invalid Password", "Credential Failed");
                return false;
            }

            return true;
        }

        //Load List
        private void LoadList()
        {
            lstFiles.Items.Clear();
            foreach (string _File in Directory.GetFiles(sp.EncryptedFolder))
            {
                lstFiles.Items.Add(Path.GetFileName(_File));
            }
        }

        //Add file to vault
        private void btnImport_Click(object sender, EventArgs e)
        {
            DialogResult dls = ofd.ShowDialog();
            if (dls == DialogResult.OK)
            {
                if (ofd.CheckFileExists)
                {
                    sp.AddFile(ofd.FileName);
                    LoadList();
                }
            }
        }

        //Encrypt single file
        private void btnEncIndFile_Click(object sender, EventArgs e)
        {
            DialogResult dls = ofd.ShowDialog();
            if (dls == DialogResult.OK)
            {
                if (ofd.CheckFileExists)
                {
                    sp.EncryptSingle(ofd.FileName, tmp);
                    MessageBox.Show("File Encrypted");
                }
            }
        }

        //Decrypt Individual File
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            DialogResult dls = ofd.ShowDialog();
            if (dls == DialogResult.OK)
            {
                if (ofd.CheckFileExists)
                {
                    sp.DecryptSingle(ofd.FileName, tmp);
                    MessageBox.Show("File Decrypted");
                }
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {

        }
    }
}
